<script setup>
import { RouterLink, RouterView } from 'vue-router'
import { useCounterStore } from '@/stores/counter'
import axios from 'axios'
import { ref,onMounted ,watchEffect } from 'vue'
import router from './router'

const userStore = useCounterStore()



// 从Pinia加载用户信息（因为开启了persist，Pinia会自动从localStorage恢复）
const loadUserInfo = () => {
  // 无需手动解析localStorage，Pinia的persist会自动处理
  console.log('App加载用户信息:', userStore.username)
}
onMounted(async() => {
   loadUserInfo()
})

// 监听Pinia状态变化，实时更新界面
watchEffect(() => {
  // 当Pinia中的用户信息变化时，自动触发界面更新
  console.log('用户信息变化:', userStore.username, userStore.avatar)
})


//退出登录
const handleLogout = () => {
  // 清空token
  userStore.token = ''
  userStore.userId = ''
  userStore.username = ''
  userStore.avatar = ''
  //刷新并跳转
  window.location.href = '/login'
}
//个人中心
const handleProfile = () => {
  router.push('/My')
}

</script>

<template>
  <el-menu
    
    class="el-menu-demo"
    mode="horizontal"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
    
  >
<el-text class="title">山猫机械网</el-text>
    <el-menu-item @click="router.push('/SimpleCarousel')" class="el-menu-item">首页</el-menu-item>
          <el-menu-item @click="router.push('/maiche')" class="el-menu-item">我要买</el-menu-item>
          <el-menu-item @click="router.push('/maishang')" class="el-menu-item">我要卖</el-menu-item>   
          <el-menu-item @click="router.push('/qiugou1')" class="el-menu-item">求购总览</el-menu-item>      
    <div class="user-profile">
    <!-- 头像+姓名组合 -->
    <el-popover
      placement="bottom-end"
      trigger="hover"
      :width="160"
      popper-class="user-menu-popper"
    >
      <template #reference>
        <div class="profile-trigger">
          <el-avatar :src="userStore.avatar"  />
          <span class="user-name" v-if="userStore.username">{{ userStore.username }}</span>
          <span class="user-name" v-else>未登录</span>
          <el-icon></el-icon>
        </div>
      </template>

      <!-- 下拉菜单内容 -->
      <div class="user-menu">
        <div class="menu-item" @click="handleProfile">
          <el-icon></el-icon>
          <span>个人中心</span>
        </div>
      
        <el-divider />
        <div class="menu-item logout" @click="handleLogout">
          <el-icon></el-icon>
          <el-text v-if="userStore.token">退出登录</el-text>
          <el-text v-else>登录</el-text>
        </div>
      </div>
    </el-popover>
  </div>
  </el-menu>
  <el-main class="el-main">
    <router-view />
  </el-main>
</template>

<style scoped>
.el-menu-item{
  color: white;
  font-size: 20px;
}
.title{
  color: white;
}
.el-main {
padding: 0 !important;  /* 移除Element Plus默认内边距 */
  margin-top: 0 !important;
  overflow: hidden !important;
  }
/* 用户头像+姓名组合 */
.user-profile {
  display: inline-block;
  margin-left: 55%;
  margin-top: 5px;
}

.profile-trigger {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.profile-trigger:hover {
  background-color: var(--el-color-primary-light-9);
}

.user-name {
  font-size: 14px;
  font-weight: 500;
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-menu {
  padding: 4px 0;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.menu-item:hover {
  background-color: var(--el-color-primary-light-9);
}

.menu-item .el-icon {
  margin-right: 8px;
  font-size: 16px;
}

.menu-item.logout {
  color: var(--el-color-danger);
}

.menu-item.logout .el-icon {
  color: var(--el-color-danger);
}
</style>

<style>
/* 全局覆盖popover样式 */
.user-menu-popper.el-popover {
  padding: 0 !important;
  margin-top: 4px !important;
}
</style>
